/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "iccreader_service.h"
#include <memory>
#include <unistd.h>
#include "file_ex.h"
#include "system_ability_definition.h"
#include "iccreader_hilog.h"
#include "iccreader_errors.h"
#include "iccreader_common.h"
#include "cashdevice_status.h"
#include "iccreader_adaptor.h"

namespace SwanLink {
namespace Finance {
namespace {
const bool REGISTER_RESULT =
    SystemAbility::MakeAndRegisterAbility(DelayedSpSingleton<IccreaderService>::GetInstance().GetRefPtr());
}

IccreaderService::IccreaderService()
    : SystemAbility(FINANCE_ICCREADER_SERVICE_ID, true),
    ready_(false)
{
}

IccreaderService::~IccreaderService() {}

void IccreaderService::OnDump()
{
    
}

void IccreaderService::OnStart()
{
    ICCREADER_HILOGD(ICCREADER_SRV, "Enter");
    if (ready_) {
        ICCREADER_HILOGD(ICCREADER_SRV, "Service is ready, nothing to do");
        return;
    }
    if (!(Init())) {
        ICCREADER_HILOGE(ICCREADER_SRV, "Call init failed");
        return;
    }

    if (!Publish(this)) {
        ICCREADER_HILOGE(ICCREADER_SRV, "Register to system ability manager failed");
        return;
    }
    ready_ = true;
}

bool IccreaderService::Init()
{
    ICCREADER_HILOGD(ICCREADER_SRV, "Enter");
    AddStatusList(g_sErr);
    OnInit(CASH_DEVICE_ICCREADER, "ICCReader");
    ICCREADER_HILOGI(ICCREADER_SRV, "Success");
    return true;
}

void IccreaderService::OnStop()
{
    ICCREADER_HILOGW(ICCREADER_SRV, "Enter");
    OnFree();
    ICCREADER_HILOGW(ICCREADER_SRV, "Success");
}

int32_t IccreaderService::Dump(int32_t fd, const std::vector<std::u16string> &args)
{
    return ERR_OK;
}


int IccreaderService::GetDeviceInfo(int fd, DeviceInfo &devInfo)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->GetDeviceInfo(devInfo);
}

int IccreaderService::IccIOCtrl(int fd,IOType type,int value)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->IccIOCtrl(type, value);
}

int IccreaderService::ApduCommand(int fd,IOType type,int len,uint8_t cmd[],uint8_t cmd_out[])
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->ApduCommand(type, len, cmd, cmd_out);
}

int IccreaderService::CardSearch(int fd,int timeout,SearchResult *search)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->CardSearch(timeout, search);
}

int IccreaderService::CardStopSearch(int fd)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->CardStopSearch();
}

int IccreaderService::RfCardActive(int fd,RfCardInfo &cardInfo)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->RfCardActive(cardInfo);
}

int IccreaderService::RfCardDormancy(int fd)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->RfCardDormancy();
}

int IccreaderService::ScanData(int fd,const DeviceConfig& config, ResultData &out_data)
{
    std::shared_ptr<IccReaderAdaptor> adaptor = std::static_pointer_cast<IccReaderAdaptor>(GetAdaptor(fd));
    if (adaptor == nullptr)
        return HANDLE_INVALID;
    return adaptor->ScanData(config, out_data);
}

} // namespace Finance
} // namespace SwanLink
